<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="title" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="parkInfoform" label-width="100px" :rules="rules" :model="parkInfo">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="parkInfo.personName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="parkInfo.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="parkInfo.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="parkInfo.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="payinfoform" label-width="100px" :rules="payRules" :model="payInfo">
            <el-form-item label="有效日期" prop="parkstatueDate">
              <el-date-picker
                v-model="payInfo.parkstatueDate"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="payInfo.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="payInfo.paymentMethod">
                <el-option
                  v-for="item in payStatus"
                  :key="item.id"
                  :value="item.value"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetPack">重置</el-button>
        <el-button type="primary" @click="addParkinfo">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addParkInfoApi, singleEditPackInfo, singlePackInfo } from '@/apis/Car'
import { Message } from 'element-ui'
import { carNumberCheck } from '@/utils/check'
export default {
  data() {
    return {
      // 车辆信息
      parkInfo: {
        personName: '',
        phoneNumber: '13856221455',
        carNumber: '京A30569',
        carBrand: ''
      },
      //   缴费信息
      payInfo: {
        parkstatueDate: [],
        paymentAmount: '',
        paymentMethod: '',
        rechargeId: ''
      },
      // 支付方式枚举
      // 支付方式,支付宝:Alipay,微信:WeChat,线下:Cash  /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
      payStatus: [
        { id: 0, value: 'Alipay', name: '支付宝' },
        { id: 1, value: 'WeChat', name: '微信' },
        { id: 2, value: 'Cash', name: '线下' }
      ],
      //   车辆校验信息
      rules: {
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入正确的联系方式', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '请输入车辆号码', trigger: 'blur' },
          { validator: carNumberCheck, trigger: 'blur' }
        ],
        carBrand: [
          { required: true, message: '请输入车辆品牌', trigger: 'blur' }
        ]
      },
      //   缴费校验信息
      payRules: {
        parkstatueDate: [
          { required: true, message: '请输入有效信息', trigger: 'blur' }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请输入支付方式', trigger: 'blur' }
        ]

      },
      //  id值
      id: this.$route.params.id
    }
  },
  computed: {
    title: {
      get() {
        if (this.$route.query.id) {
          return '编辑月卡'
        } else {
          return '添加月卡'
        }
      }
    }
  },
  async created() {
    if (this.$route.query.id) {
      const res = await singlePackInfo(this.$route.query.id)
      for (const key in this.parkInfo) {
        this.parkInfo[key] = res.data[key]
      }

      for (const key in this.payInfo) {
        if (key === 'parkstatueDate') {
          this.payInfo.parkstatueDate.push(res.data.rechargeList[0].cardStartDate)
          this.payInfo.parkstatueDate.push(res.data.rechargeList[0].cardEndDate)
          continue
        }
        this.payInfo[key] = res.data.rechargeList[0][key]
      }
    }
  },
  methods: {
    // 添加月卡信息
    async addParkinfo() {
      this.$refs.parkInfoform.validate((isQdopt) => {
        if (!isQdopt) {
          Message.error('请填写正确的车辆信息字段')
          return
        }
        this.$refs.payinfoform.validate(async(isQdopt) => {
          if (!isQdopt) {
            Message.error('请填写正确的月卡信息字段')
            return
          }
          const obj = {
            ...this.parkInfo,
            ...this.payInfo
          }
          obj.cardStartDate = this.payInfo.parkstatueDate[0]
          obj.cardEndDate = this.payInfo.parkstatueDate[1]
          delete obj.parkstatueDate

          if (!this.$route.query.id) {
            console.log(obj)
            delete obj.rechargeId
            await addParkInfoApi(obj)
            Message.success('添加成功！')
          } else {
            obj.carInfoId = this.$route.query.id
            obj.rechargeId = this.payInfo.rechargeId
            console.log(obj)
            await singleEditPackInfo(obj)
            Message.success('编辑成功！')
          }
          this.$router.back()
        })
      })
    },
    // 重置表单
    resetPack() {
      this.$refs.parkInfoform.resetFields()
      this.$refs.payinfoform.resetFields()
    }
  }
}
</script>

  <style scoped lang="scss">
  .add-card {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      align-items: center;
      padding: 0 20px;
      height: 64px;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }
        .arrow{
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      background: #f4f6f8;
      padding: 20px 130px;

      .form-container {
        background-color: #fff;

        .title {
          height: 60px;
          line-height: 60px;
          padding-left: 20px;
        }

        .form {
          margin-bottom: 20px;
          padding: 20px 65px 24px;

          .el-form {
            display: flex;
            flex-wrap: wrap;

            .el-form-item {
              width: 50%;
            }
          }
        }
      }
      .preview{
        img{
          width: 100px;
        }
      }
    }

    .add-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      padding: 24px 50px;
      color: #000000d9;
      font-size: 14px;
      background: #fff;
      text-align: center;
    }
  }
  </style>
